﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using SPSAndong.Business;
using SPSAndong.Model;
using SPSAndong.Comm;

namespace SPSAndong.Forms
{
    public partial class FrmCallRecord : UIPage
    {
        CallDetailsBLL callDetailsBLL = new CallDetailsBLL();
        ProjectSetBLL ProjectSetBLL = new ProjectSetBLL();
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;
        /// <summary>
        /// 数据表每页行数
        /// </summary>
        int pageSize = 30;
        public FrmCallRecord()
        {
            InitializeComponent();
        }

        private void FrmExceptionRecord_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            //设置开始时间为一星期前
            this.dpStartDate.Value = DateTime.Now.AddDays(-2);
            this.dpEndDate.Value = DateTime.Now;

            isLoad = false;

            uiPagination1.Style = UIStyle.Blue;
            uiPagination1.ShowJumpButton = true;

            dgvHistory.AutoGenerateColumns = false;
            dgvHistory.AllowUserToAddRows = false;
            dgvHistory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHistory.ReadOnly = true;
            dgvHistory.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dgvHistory.AddColumn("序号", "", 15);
            dgvHistory.AddColumn("区域名称", "AreaName", 25);
            dgvHistory.AddColumn("工程名称", "ProjectName", 25);
            dgvHistory.AddColumn("呼叫时间", "CreateTime", 25);
            dgvHistory.AddColumn("响应时间", "ResponseTime", 25);
            GetCbName();
            cbType.SelectedIndex = 0;
            Search();

        }

        private void GetCbName()
        {
            var pslist = ProjectSetBLL.GetModelAll();
            pslist.Insert(0, new ProjectSetModel() { WorkName = "全部" });
            cbName.DataSource = pslist;
            cbName.ValueMember = "WorkName";
            cbName.DisplayMember = "WorkName";
        }

        private void FrmExceptionRecord_SizeChanged(object sender, EventArgs e)
        {

        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            string deviceType = "";
            string projectName = "";
            DateTime startDate = dpStartDate.Value;
            DateTime endDate = dpEndDate.Value;
            if (!cbType.Text.Equals("全部"))
            {
                deviceType = cbType.Text;
            }

            if (cbName.SelectedIndex > 0)
            {
                var projectSetModel = cbName.SelectedItem as ProjectSetModel;
                projectName = projectSetModel?.WorkName;
            }
            dgvHistory.DataSource = callDetailsBLL.GetModelListBySql(deviceType, projectName, startDate, endDate, pageIndex, count);
            Dgv_Cells(ref dgvHistory);
            dgvHistory.Refresh();
        }

        public void Dgv_Cells(ref UIDataGridView dgv)
        {
            if (dgv.Rows.Count > 0)
                dgv.Rows[0].Selected = false;

            DateTime currentTime = DateTime.Now.ToLocalTime();
            DateTime startTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 7, 30, 0); // 早上7点30分
            DateTime endTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 16, 59, 59); // 下午5点前

            foreach (DataGridViewRow item in dgv.Rows)
            {
                var model = item.DataBoundItem as CallDetailsModel;
                //B班，背景色为橙色
                if (PublicHelper.TimeCompare(model.CreateTime, startTime) == 2 || PublicHelper.TimeCompare(model.CreateTime, endTime) == 1)
                {
                    item.DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 128);
                }
            }
        }


        private void btnQueryJD_Click(object sender, EventArgs e)
        {
            Search();
        }

        /// <summary>
        /// 查询
        /// </summary>
        private void Search()
        {
            string deviceType = "";
            string projectName = "";
            DateTime startDate = dpStartDate.Value;
            DateTime endDate = dpEndDate.Value;
            if (!cbType.Text.Equals("全部"))
            {
                deviceType = cbType.Text;
            }

            if (cbName.SelectedIndex > 0)
            {
                var projectSetModel = cbName.SelectedItem as ProjectSetModel;
                projectName = projectSetModel?.WorkName;
            }

            uiPagination1.TotalCount = callDetailsBLL.GetModelCountByWhere(deviceType, projectName, startDate, endDate);
            uiPagination1.ActivePage = 1;
            uiPagination1_PageChanged(null, null, 1, pageSize);
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbType.SelectedIndex <= 0)
            {
                GetCbName();
                return;
            }

            var areaName = cbType.Text;
            var pslist = ProjectSetBLL.GetListByAreaName(areaName);
            pslist.Insert(0, new ProjectSetModel() { WorkName = "全部" });
            cbName.DataSource = pslist;

            //Search();
        }

        private void dgvHistory_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvHistory.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvHistory.RowHeadersDefaultCellStyle.Font, rect, dgvHistory.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

    }
}
